#include "worker.h"

//公证员界面
int workerFace()
{
    while(1)
    {
        system("clear");
        printf("请选择需要的功能:\n");
        printf("1.开奖\n");
        printf("2.查询彩票信息\n");
        printf("0.返回登录界面\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2&&iCho!=0)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//公证员选择界面
void switchByWorker()
{
    int ret=workerFace();
    switch (ret)
    {
    case 1:
        openLottery();
        switchByWorker();
        break;
    case 2:
        displayAllLottery();
        switchByWorker();
        break;
    case 0:
        loginSwitch();
        break;
    default:
        break;
    }
}
 
//开奖
void openLottery()
{
    lyList *issueList=mkLyList();
    loadLotteryInfoFromFile(LOTTERY_PATH,issueList);
    lyNode *node=findNotOpenLyNode(issueList);
    if(node == NULL)
    {
        printf("新一期彩票还未发布！请等管理员发布后再下注!\n");
        huichejixu();
        releaseLyList(NULL,issueList);
        return;
    }
    else if (node->sum_num == 0)
    {
        printf("新一期彩票还没有人购买！请等土豪氪金后再开！\n");
        huichejixu();
        releaseLyList(NULL,issueList);
        return;
    }
    else
    {
        switchByLottery(node);
        strcpy(node->openstatus,HAVE_OPEN);

        usrList *usrInfoList=mkList();
        loadInfoFromFile(PATH_NAME,usrInfoList);

        buyLyList *boughtList=mkBuyLyList();
        loadBuyLotteryInfoFromFile(BUYLOTTERY_PATH,boughtList);


        buyLyNode *boughtNode=boughtList->pFirstNode;
        while(boughtNode != NULL)
        {
            if(boughtNode->issue ==node->issue)
            {
                int i=0;
                int flag=0;
                for(i=0;i<LOTTERY_LEN;i++)
                {
                    if(boughtNode->lottery[i]==node->lottery[i])
                    {
                        flag++;
                    }
                }
                if(flag == 6)
                {
                    usrNode *usrTempNode= findNodeByName(usrInfoList,boughtNode->player);
                    usrTempNode->usrMoney=usrTempNode->usrMoney+(node->price * boughtNode->count);
                    boughtNode->money=node->price * boughtNode->count;
                }
                strcpy(boughtNode->openstatus,HAVE_OPEN);
            }
            boughtNode=boughtNode->pnext;
        }
        saveLotteryListToFile(LOTTERY_PATH,issueList);
        saveBuyLotteryListToFile(BUYLOTTERY_PATH,boughtList);
        saveListToFile(PATH_NAME,usrInfoList);

        releaseBuyLyList(NULL,boughtList);
        releaseLyList(NULL,issueList);
        releaseUsrList(NULL,usrInfoList);

        printf("开奖成功\n");
        huichejixu();
    }
    
    
}

//查看所有彩票
void displayAllLottery()
{
    lyList *list=mkLyList();
    loadLotteryInfoFromFile(LOTTERY_PATH,list);
    lyNode *node=list->pFirstNode;
    printf("期号\t中奖号\t单价\t总注数\t总额\t开奖状态\n");
    while(node != NULL)
    {
        printf("%d\t",node->issue);
        int i=0;
        for(i=0;i<LOTTERY_LEN;i++)
        {
            printf("%d",node->lottery[i]);
        }
        printf("\t%.2lf\t%d\t%.2lf\t%s\n",node->price,node->sum_num,node->sum_mon,node->openstatus);
        node=node->pnext;
    }
    releaseLyList(NULL,list);
    huichejixu();
}

//生成彩票界面
int getLotteryFace()
{
    while(1)
    {
        system("clear");
        printf("请选择需要的功能:\n");
        printf("1.手动生成\n");
        printf("2.自动生成\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//生成彩票选择界面
void switchByLottery(lyNode *node)
{
    int ret=getLotteryFace();
    switch (ret)
    {
    case 1:
        manualLottery(node);
        break;
    case 2:
        autoLottery(node);
        break;
    default:
        break;
    }
}

//手动生成彩票
void manualLottery(lyNode *node)
{
    int i=0;
    for(i=0;i<LOTTERY_LEN;i++)
    {
        printf("请输入第%d个号码:\n",i+1);
        scanf("%d",&(node->lottery[i]));
    }
}

//自动生成彩票
void autoLottery(lyNode * node)
{
    int pstr[]={0,1,2,3,4,5,6,7,8,9};
    srand(time(0));
    node->lottery[0]=pstr[rand()%10];
    node->lottery[1]=pstr[rand()%10];
    node->lottery[2]=pstr[rand()%10];
    node->lottery[3]=pstr[rand()%10];
    node->lottery[4]=pstr[rand()%10];
    node->lottery[5]=pstr[rand()%10];
}